/**
 * 播放通知音频
 */
import storages from '@gnlab/utils/env-browser/storages'
import throttle from 'lodash/throttle'
import file from '@/desktop/assets/notify.mp3'

/**
 * 控制播放开关
 */

const storageKey = 'quntool-enable-audio-notify'
let enable = !!(storages.local.get(storageKey, 0) as 0 | 1)
export function getEnableNotify() {
  return enable
}
export function setEnableNotify(_enable: boolean) {
  enable = _enable
  storages.local.set(storageKey, enable ? 1 : 0)
}

/**
 * 实现播放
 */
const audio = new Audio(file)

// 限制频率频率播放
export const playNotifyAudio = throttle(realPlayNotifyAudio, 1000)

// 无条件触发播放
export async function realPlayNotifyAudio() {
  if (enable) return audio.play()
}
